package com.jiuzhou.guanwang.jzcp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.bean.TeamInfo;

import java.util.List;

public class TeamRankAdapter extends BaseAdapter {
    private final Context mContext;
    private List<TeamInfo> mData;
    public static final String PATHS_SEPARATOR = "/";
    class ViewHolder {
        TextView detailTv;
        ImageView iconIv;
        TextView matchIv;
        TextView nameTv;
        TextView pointTv;
        TextView rankTv;

        ViewHolder() {
        }
    }

    public TeamRankAdapter(Context context) {
        this.mContext = context;
    }

    public int getCount() {
        return this.mData == null ? 0 : this.mData.size();
    }

    public Object getItem(int position) {
        return this.mData == null ? null : (TeamInfo) this.mData.get(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.item_team_rank, parent, false);
            holder.rankTv = (TextView) convertView.findViewById(R.id.tv_rank);
            holder.iconIv = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.matchIv = (TextView) convertView.findViewById(R.id.tv_match);
            holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name);
            holder.detailTv = (TextView) convertView.findViewById(R.id.tv_detail);
            holder.pointTv = (TextView) convertView.findViewById(R.id.tv_point);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TeamInfo teamInfo = (TeamInfo) this.mData.get(position);
        if (teamInfo != null) {
            if (!TextUtils.isEmpty(teamInfo.rank)) {
                holder.rankTv.setText(teamInfo.rank);
            }
            if (!TextUtils.isEmpty(teamInfo.teamLogo)) {
                Glide.with(this.mContext).load(teamInfo.teamLogo).into(holder.iconIv);
            }
            if (!TextUtils.isEmpty(teamInfo.team)) {
                holder.nameTv.setText(teamInfo.team);
            }
            StringBuilder builder = new StringBuilder();
            if (TextUtils.isEmpty(teamInfo.wins)) {
                builder.append("");
                builder.append(PATHS_SEPARATOR);
            } else {
                builder.append(teamInfo.wins);
                builder.append(PATHS_SEPARATOR);
            }
            if (TextUtils.isEmpty(teamInfo.draws)) {
                builder.append("");
                builder.append(PATHS_SEPARATOR);
            } else {
                builder.append(teamInfo.draws);
                builder.append(PATHS_SEPARATOR);
            }
            if (TextUtils.isEmpty(teamInfo.losses)) {
                builder.append("");
            } else {
                builder.append(teamInfo.losses);
            }
            holder.detailTv.setText(builder);
            if (!TextUtils.isEmpty(teamInfo.points)) {
                holder.pointTv.setText(teamInfo.points);
            }
            if (!TextUtils.isEmpty(teamInfo.matchesPlayed)) {
                holder.matchIv.setText(teamInfo.matchesPlayed);
            }
        }
        return convertView;
    }

    public void setData(List<TeamInfo> teamInfo) {
        this.mData = teamInfo;
        notifyDataSetChanged();
    }
}